﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace UDS.Components
{
    public class BBSForumItem
    {
        public int ItemID
        {
            get;
            set;
        }

        public int BoardID
        {
            get;
            set;
        }

        public string Title
        {
            get;
            set;
        }

        public string Content
        {
            get;
            set;
        }

        public string Sender
        {
            get;
            set;
        }

        /// <summary>
        /// 发送时间
        /// </summary>
        public DateTime SendTime
        {
            get;
            set;
        }

        public int HitTimes
        {
            get;
            set;
        }

        /// <summary>
        /// 回复次数
        /// </summary>
        public int ReplayTimes
        {
            get;
            set;
        }

        public string LastReplayer
        {
            get;
            set;
        }

        public string LastReplayTime
        {
            get;
            set;
        }

        public string IP
        {
            get;
            set;
        }

        /// <summary>
        /// 版面公告
        /// </summary>
        public bool Bulletin
        {
            get;
            set;
        }

        /// <summary>
        /// 系统公告
        /// </summary>
        public bool SysBulletin
        {
            get;
            set;
        }

        /// <summary>
        /// 公告桌面显示
        /// </summary>
        public bool DeskTop
        {
            get;
            set;
        }

        /// <summary>
        /// 将贴子附件与自身绑定
        /// </summary>
        /// <param name="filenames">文件名（多个文件名用 , 分开）</param>
        public void Attach(string filenames)
        {
            var data = new Database();
            string[] arrfilename = filenames.Split(',');
            for (int i = 0; i < arrfilename.Length; i++)
            {
                if (arrfilename[i].Trim() != "")
                {
                    var parameters = new[]
                    {
                        Database.MakeInParam("@itemid", SqlDbType.Int, 4, ItemID),
                        Database.MakeInParam("@filename", SqlDbType.NVarChar, 500, arrfilename[i])
                    };
                    data.RunProc("dbo.sproc_BBS_AttachmentToItem", parameters);
                }
            }
        }

        /// <summary>
        /// 删除附件
        /// </summary>
        public void DelAttachment(string attachmentmd)
        {
            var data = new Database();

            //删除所有回复附件
            //得到所有回复
            var bbs = new BBSClass();
            using (SqlDataReader dr = bbs.ReadBBSForumItemReplay(this))
            {
                while (dr.Read())
                {
                    var replay = new BBSReplay();
                    replay.ReplayId = Int32.Parse(dr["replay_id"].ToString());
                    replay.DelAttachment(attachmentmd);
                }
            }

            //删除贴子本身附件
            var parameters = new[]
            {
                Database.MakeInParam("@itemid", SqlDbType.Int, 4, ItemID),
            };
            using (SqlDataReader dr = data.RunDataReader("dbo.sproc_BBS_GetItemAttachment", parameters))
            {
                while (dr.Read())
                {
                    File.Delete(attachmentmd + "\\" + dr["filename"]);
                }
            }

            var prams1 = new[]
            {
                Database.MakeInParam("@itemid", SqlDbType.Int, 4, ItemID),
            };
            data.RunProc("dbo.sproc_BBS_DelAttachmentToItem", prams1);
        }
    }
}